package com.zyf.school.manager.service.impl;

import com.zyf.school.manager.dao.DictDao;
import com.zyf.school.manager.domain.Dict;
import com.zyf.school.manager.domain.DictTree;
import com.zyf.school.manager.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl implements DictService {
    @Autowired
    private DictDao dictDao;
    private List<Dict> list;
    private List<DictTree> dictTreeList;

    @Override
    public int count() {
        return dictDao.count();
    }

    @Override
    public List<Dict> queryByParentid(int parentid, int startIndex, int pageSize) {
        list = dictDao.queryByParentId(parentid,startIndex,pageSize);
        list = setList(list);
        return list;
    }

    @Override
    public Dict insertDict(Dict dict) {
        dictDao.insert(dict);
        return dict;
    }

    @Override
    public List<Dict> queryByType(String type, int startIndex, int pageSize) {
        list = dictDao.queryByType(type,startIndex,pageSize);
        list = setList(list);
        return list;
    }

    @Override
    public List<Dict> queryAll(int startIndex, int pageSize) {
        list = dictDao.queryAll(startIndex,pageSize);
        list = setList(list);
        return list;
    }

    @Override
    public List<Dict> queryAllParent(int startIndex, int pageSize) {
        list = dictDao.queryAllParent(startIndex,pageSize);
        list = setList(list);
        return list;
    }

    public List<Dict> setList(List<Dict> al){
        for (int i=0; i<al.size(); i++){
            list = dictDao.queryAllById(al.get(i).getId());
            al.get(i).setList(list);
            setList(list);
        }
        return al;
    }

    public List<Dict> queryById(List<Dict> list){
        if(list.size()>0) {
            for (int i = 0; i < list.size(); i++) {
                Dict d = list.get(i);
                d.setList(dictDao.queryByParentId(d.getId(), 0, 100));
                list = queryById(d.getList());
            }
        }
        return list;
    }
}
